#include "shopdialog.h"
#include<QPainter>
#include "playertable.h"
#include "gamedialog.h"

/*

  SHOP BACKGROUND CLASS

*/
ShopBackground::ShopBackground(QGraphicsObject *parent)
    :QGraphicsObject(parent)
{
    setOpacity(0);
}

QRectF ShopBackground::boundingRect() const{
    return QRectF(-ShopDialog::SHOP_WIDTH/2,
                  -ShopDialog::SHOP_HEIGHT/2,
                  ShopDialog::SHOP_WIDTH, ShopDialog::SHOP_HEIGHT);
}

void ShopBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
}

/*

  SHOP TEXT INFO CLASS

*/
ShopTextInfo::ShopTextInfo(QGraphicsObject *parent)
    :QGraphicsObject(parent)
{
    setOpacity(0);
}

QRectF ShopTextInfo::boundingRect() const{
    return QRectF(-ShopDialog::SHOP_WIDTH/2+10+ShopOwner::SHOP_OWNER_WIDTH,
                  ShopDialog::SHOP_WIDTH/2-10-ShopOwner::SHOP_OWNER_HEIGHT,
                  SHOP_TEXT_WIDTH, ShopOwner::SHOP_OWNER_HEIGHT);
}

void ShopTextInfo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::red);
    painter->drawText(boundingRect(), Qt::AlignLeft, " Text area\n Welcome to teashop\n (click to close to finish shopping)");
}

/*

  SHOP OWNER CLASS

*/




ShopOwner::ShopOwner(QGraphicsObject *parent)
    :QGraphicsObject(parent)
{
    setOpacity(0);
}

QRectF ShopOwner::boundingRect() const{
    return QRectF(-ShopDialog::SHOP_WIDTH/2+10, ShopDialog::SHOP_WIDTH/2-10-SHOP_OWNER_HEIGHT,
                  SHOP_OWNER_WIDTH, SHOP_OWNER_HEIGHT);
}

void ShopOwner::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::blue);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Owner head");
}

/*

  SHOP OWNER BIG CLASS

*/




ShopOwnerBig::ShopOwnerBig(QGraphicsObject *parent, const QPixmap &pix)
    :QGraphicsObject(parent), _pix(pix)
{
    setOpacity(0);
}

QRectF ShopOwnerBig::boundingRect() const{
    return QRectF(-_pix.width()/2, -_pix.height()/2, _pix.width(), _pix.height());
}

void ShopOwnerBig::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
}


/*

  SHOP DIALOG CLASS


  */



ShopDialog::ShopDialog(QGraphicsObject* parent)
    :QGraphicsObject(parent)
{

    foreScene = new ForegroundScene(parent);
    foreScene->setZValue(zValue()+2);
    this->setZValue(zValue()+3);


    //the shop dialog is initially invisible
    this->setVisible(false);

}


QRectF ShopDialog::boundingRect() const{
    return QRectF(-SHOP_WIDTH/2, -SHOP_HEIGHT/2, SHOP_WIDTH, SHOP_HEIGHT);
}

void ShopDialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

}

void ShopDialog::createAnimation(){
    enterAnim = new GameSeqAnimation(this);

    GameParAnimation* enterBack = new GameParAnimation(enterAnim);
    enterBack->addFade(back, 0, 1, 200);

    //QPointF targetPos = ownerBig->pos();
    //ownerBig->setPos(20, 0);
    GameParAnimation* enterOwner = new GameParAnimation(enterAnim);
    enterOwner->addFade(ownerBig, 0, 1, 300);
    enterOwner->addMove(ownerBig, QPointF(20,0), QPointF(0,0), 300);

    GameParAnimation* enterText = new GameParAnimation(enterAnim);
    enterText->addFade(owner, 0, 1, 200);
    enterText->addFade(text, 0, 1, 200);

    enterAnim->addAnimation(enterBack);
    enterAnim->addAnimation(enterOwner);
    enterAnim->addAnimation(enterText);

    connect(enterAnim, SIGNAL(finished()), this, SLOT(waitForClickEnter()));
}

void ShopDialog::prepare(){

    this->setVisible(true);
    foreScene->setVisible(true);
    back->setOpacity(0);
    owner->setOpacity(0);
    ownerBig->setOpacity(0);
    text->setOpacity(0);
}

void ShopDialog::enter(){
    prepare();

    enterAnim->start();
}

void ShopDialog::waitForClickEnter(){
    SceneSensor* s = new SceneSensor(this);
    connect(s, SIGNAL(pressed()), this, SLOT(shop()));
}

void ShopDialog::shop(){
    shopping();
}

void ShopDialog::waitForClickExit(){
    SceneSensor* s = new SceneSensor(this);
    connect(s, SIGNAL(pressed()), this, SLOT(leave()));
    //clean up the dice
    connect(s, SIGNAL(pressed()), this, SIGNAL(readyToCleanUp()));
}

void ShopDialog::leave(){
    //exit animation
    GameParAnimation* exit = new GameParAnimation(this);
    exit->addFade(this, 1, 0, 300);
    exit->addFade(foreScene, 0.5, 0, 300);
    exit->start(QAbstractAnimation::DeleteWhenStopped);
    connect(exit, SIGNAL(finished()), this, SLOT(finished()));
}

void ShopDialog::finished(){
    //stuff after anim finished
    foreScene->setVisible(false);
    this->setVisible(false);
    this->setOpacity(1);
}

/*

  TEA SHOP CLASS

*/

TeashopDialog::TeashopDialog(QGraphicsObject *parent, const QPixmap &pix)
    :ShopDialog(parent)
{

    //components instantiate
    back = new ShopBackground(this);
    ownerBig = new ShopOwnerBig(this, pix);
    owner = new ShopOwner(this);
    text = new ShopTextInfo(this);

    createAnimation();
}


void TeashopDialog::shopping(){
    DiceManager* dm = new DiceManager(this);
    dm->addDice();
    dm->addDice();
    dm->addDice();
    connect(dm, SIGNAL(finished()), this, SLOT(waitForClickExit()));
    connect(this, SIGNAL(readyToCleanUp()), dm, SLOT(deleteLater()));
}


/*


  SORCERER SHOP CLASS


  */
SorcererDialog::SorcererDialog(QGraphicsObject *parent, const QPixmap &pix)
    :ShopDialog(parent)
{
    back = new ShopBackground(this);
    ownerBig = new ShopOwnerBig(this, pix);
    owner = new ShopOwner(this);
    text = new ShopTextInfo(this);

    createAnimation();
}

/*

  ShopDialog
       parent: TileBoard
            parent: PlayerTable

  */

void SorcererDialog::shopping(){
    this->setVisible(false);
    foreScene->setVisible(false);

    ActionGuideDialog g(this->parentObject(), "Choose a ghost slot");
    PlayerTable* table = dynamic_cast<PlayerTable*>(this->parentObject()->parentObject());
    GhostSlot* clicked = table->askForSlot();
    //later could be clicked->killAGhost(), sort of stuff like that
    clicked->setVisible(false);
    foreScene->setVisible(true);
    this->setVisible(true);
    waitForClickExit();
}


/*


  ALTAR SHOP CLASS


  */
AltarDialog::AltarDialog(QGraphicsObject *parent, const QPixmap &pix)
    :ShopDialog(parent)
{
    back = new ShopBackground(this);
    ownerBig = new ShopOwnerBig(this, pix);
    owner = new ShopOwner(this);
    text = new ShopTextInfo(this);

    createAnimation();
}


void AltarDialog::shopping(){
    this->setVisible(false);
    foreScene->setVisible(false);

    ActionGuideDialog g(this->parentObject(), "Choose a village to nullify");
    PlayerTable* table = dynamic_cast<PlayerTable*>(this->parentObject()->parentObject());
    VillageTile* clicked = table->askForVillageTile();
    //later could be clicked->killAGhost(), sort of stuff like that
    clicked->setVisible(false);
    foreScene->setVisible(true);
    this->setVisible(true);
    waitForClickExit();
}


/*


  PAVILION SHOP CLASS


  */
PavilionDialog::PavilionDialog(QGraphicsObject *parent, const QPixmap &pix)
    :ShopDialog(parent)
{
    back = new ShopBackground(this);
    ownerBig = new ShopOwnerBig(this, pix);
    owner = new ShopOwner(this);
    text = new ShopTextInfo(this);

    createAnimation();
}


void PavilionDialog::shopping(){
    this->setVisible(false);
    foreScene->setVisible(false);

    ActionGuideDialog g(this->parentObject(), "Choose a slot");
    PlayerTable* table = dynamic_cast<PlayerTable*>(this->parentObject()->parentObject());
    GhostSlot* first = table->askForSlot();
    first->setVisible(false);
    g.setGuideText("Choose another slot");
    GhostSlot* second = table->askForSlot();
    //then exchange the ghost in these two slots.
    second->setVisible(false);
    foreScene->setVisible(true);
    this->setVisible(true);
    waitForClickExit();
}
